#include "moveDir.h"
char* moveDir(char *sourceDirPath,char *destDirPath)
{
	if(mh==NULL)
		return ERR_VFS_MOVEDIR_08;
	if(sourceDirPath[0]!='/')
		return ERR_VFS_MOVEDIR_01;
	if(destDirPath[0]!='/')
		return ERR_VFS_MOVEDIR_02;
	descriptors *s=(descriptors*)searchBST(sourceDirPath);
	if(s!=NULL)
		return ERR_VFS_MOVEDIR_04;
	int z=strlen(sourceDirPath);
	if(z!=1 && sourceDirPath[z-1]=='/')
		sourceDirPath[z-1]='\0';
	z=strlen(destDirPath);
	if(z!=1 && destDirPath[z-1]=='/')
		destDirPath[z-1]='\0';
	descriptors *d=(descriptors*)malloc(sizeof(descriptors));
	d=(descriptors*)searchBST(destDirPath);
	if(d!=NULL)
		return ERR_VFS_MOVEDIR_07;
	char *tempSourceDirPath=(char*)malloc(sizeof(char)*strlen(sourceDirPath)+1);
	char *tempDestDirPath=(char*)malloc(sizeof(char)*strlen(destDirPath)+1);
	sprintf(tempSourceDirPath,"%s",sourceDirPath);
	sprintf(tempDestDirPath,"%s",destDirPath);
	int i=strlen(sourceDirPath)-1;
	for(;sourceDirPath[i]!='/';i--);
	char *srcParent=(char*)malloc(sizeof(char)*(i+1));
	int j=0;
	for(;j<i;j++)
		srcParent[j]=sourceDirPath[j];
	srcParent[j]='\0';
	char *src=(char*)malloc(sizeof(char)*(strlen(sourceDirPath)-1-i+1));
	j=0;
	i++;
	for(;sourceDirPath[i]!='\0';j++)
	{
		src[j]=sourceDirPath[i];
		i++;
	}
	src[j]='\0';
	if(strcmp(srcParent,"")==0 || strcmp(srcParent," ")==0)
		srcParent="/";
	i=strlen(destDirPath)-1;
	for(;destDirPath[i]!='/';i--);
	char *destParent=(char*)malloc(sizeof(char)*(i+1));
	j=0;
	for(;j<i;j++)
		destParent[j]=destDirPath[j];
	destParent[j]='\0';
	char *dest=(char*)malloc(sizeof(char)*(strlen(destDirPath)-1-i+1));
	j=0;
	i++;
	for(;destDirPath[i]!='\0';j++)
	{
		dest[j]=destDirPath[i];
		i++;
	}
	dest[j]='\0';
	if(strcmp(destParent,"")==0 || strcmp(destParent," ")==0)
		destParent="/";
	char *sourcePathTokens = NULL;
	char *destPathTokens=NULL;
	nAryNode *temp=nAryRoot;
	int found=0;
	sourcePathTokens = strtok( tempSourceDirPath, "/" );
	char *lastSourceToken=(char*)malloc(sizeof(char)*15);
	while(sourcePathTokens!=NULL)
	{
		found=0;
		if(temp==nAryRoot)
		{
			if(temp->child!=NULL)
				temp=temp->child;
			else
				return ERR_VFS_MOVEDIR_01;
		}
		else
		{
			while(temp->sibling!=NULL)
			{
				if(strcmp(temp->name,sourcePathTokens)!=0)
					temp=temp->sibling;
				else
				{
					found=1;
					break;
				}
			}
			if(strcmp(temp->name,sourcePathTokens)==0)
				found=1;
			if(!found)
				return ERR_VFS_MOVEDIR_01;
			sprintf(lastSourceToken,"%s",sourcePathTokens);
			sourcePathTokens = strtok( NULL, "/" );
			if(sourcePathTokens!=NULL && temp->child==NULL)
				return ERR_VFS_MOVEDIR_01;
			if(temp->child!=NULL)
				temp=temp->child;
		}
	}
	temp=nAryRoot;
	destPathTokens=strtok(tempDestDirPath,"/");
	found=0;
	while(destPathTokens!=NULL)
	{
		found=0;
		if(temp==nAryRoot)
		{
			if(temp->child!=NULL)
				temp=temp->child;
			else
				return ERR_VFS_MOVEDIR_02;
		}
		else
		{
			while(temp->sibling!=NULL)
			{
				if(strcmp(temp->name,destPathTokens)!=0)
					temp=temp->sibling;
				else
				{
					found=1;
					break;
				}
			}
			if(strcmp(temp->name,destPathTokens)==0)
				found=1;
			if(!found)
				return ERR_VFS_MOVEDIR_02;
			destPathTokens = strtok( NULL, "/" );
			if(destPathTokens!=NULL && temp->child==NULL)
				return ERR_VFS_MOVEDIR_02;
			if(temp->child!=NULL)
				temp=temp->child;
		}
	}
	if(strcmp(temp->name,lastSourceToken)==0)
		return ERR_VFS_MOVEDIR_05;
	else
	{
		while(temp->sibling!=NULL)
		{
			if(strcmp(temp->name,lastSourceToken)==0)
				return ERR_VFS_MOVEDIR_05;
			temp=temp->sibling;
		}
	}
	if(strcmp(sourceDirPath,"/")==0)
		return ERR_VFS_MOVEDIR_06;
	if(strncmp(sourceDirPath,destDirPath,strlen(sourceDirPath))==0)
		return ERR_VFS_MOVEDIR_06;
	int maxFileDescriptors=mh->maxDescriptors;
	int iterDescriptors=0;
	char *newLocation;
	for(;iterDescriptors<maxFileDescriptors;iterDescriptors++)
	{
		int lengthOfSrcParent=strlen(srcParent);
		int lengthOfSrcPath=strlen(sourceDirPath);
		if((strncmp(h->desc[iterDescriptors].location,sourceDirPath,lengthOfSrcPath)==0)&&(strncmp(h->desc[iterDescriptors].location,sourceDirPath,lengthOfSrcParent)==0))
		{
			newLocation=(char*)malloc(sizeof(char)*(strlen(destDirPath)+strlen(src)+1+1));
			sprintf(newLocation,"%s/%s",destDirPath,src);
			sprintf(h->desc[iterDescriptors].location,"%s",newLocation);
		}
		if(strcmp(h->desc[iterDescriptors].location,srcParent)==0 && strcmp(h->desc[iterDescriptors].fileName,src)==0)
		{
			newLocation=(char*)malloc(sizeof(char)*(strlen(destDirPath)+strlen(src)+1+1));
			sprintf(newLocation,"%s",destDirPath);
			sprintf(h->desc[iterDescriptors].location,"%s",newLocation);
		}	
	}
	int bstRet=insertBSTWhileMount(maxFileDescriptors,h->desc);
	if(bstRet!=0)
		return "FAILURE <while creating BST>";
	int nAryRet=createnAryTree(maxFileDescriptors,h->desc);
	if(nAryRet!=0)
		return "FAILURE <while creating nAryTree>";
	int hashTableRet=hashTableMethod(h->desc,maxFileDescriptors);
	if(hashTableRet!=0)
		return "FAILURE <while creating hashTable>";
	return "SUCCESS";
}
